package item.inkcloud.gateway.filter;

import com.alibaba.fastjson.JSONObject;
import item.inkcloud.gateway.feignclient.JwtFeignClient;
import item.inkcloud.gateway.utils.RequestAddParaUtils;
import item.inkcloud.gateway.utils.ResponeUtils;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpCookie;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


@Component
public class ResourceGatewayFilterFactory extends AbstractGatewayFilterFactory {

    @Autowired
    ResponeUtils responeUtils;

    @Lazy
    @Autowired
    JwtFeignClient jwtFeignClient;

    @Autowired
    RequestAddParaUtils requestAddParaUtils;

    public ResourceGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Object config) {

        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

                ServerHttpRequest request = exchange.getRequest();

                String path = request.getPath().toString();

                if (path.contains("List")) {
                    //获取资源列表，直接放行
                    chain.filter(exchange);

                } else {
                    HttpCookie tokenCookie = request.getCookies().getFirst("token");
                    HttpCookie useridCookie = request.getCookies().getFirst("userid");
                    if (tokenCookie == null || useridCookie == null) {
                        //token为空直接返回状态0
                        ServerHttpResponse response = exchange.getResponse();

                        DataBuffer buffer = responeUtils.getResponeBuffer(response);

                        return response.writeWith(Mono.just(buffer));

                    } else {

                        String token = tokenCookie.getValue();
                        String userid = useridCookie.getValue();

                        JSONObject checkPara = new JSONObject();
                        checkPara.put("token",token);
                        checkPara.put("userid",userid);

                        int status = jwtFeignClient.checkToken(checkPara);


                        if (status == 0) {
                            //鉴权失败
                            ServerHttpResponse response = exchange.getResponse();

                            DataBuffer buffer = responeUtils.getResponeBuffer(response);

                            return response.writeWith(Mono.just(buffer));
                        }
                        //鉴权通过

                        ServerHttpRequest newRequest = requestAddParaUtils.addPara(exchange, "userid", userid);
                        return chain.filter(exchange.mutate().request(newRequest).build());

                    }

                }

                return chain.filter(exchange);
            }
        };
    }





    @NoArgsConstructor
    @Data
    public static class Config{

    }

}
